//
//  TTDetailFilterDingzhiView.m
//  TaoTuo
//
//  Created by admin on 2020/3/22.
//  Copyright © 2020 admin. All rights reserved.
//

#import "TTDetailFilterDingzhiView.h"
#import "WSDatePickerView.h"

@interface TTDetailFilterDingzhiView ()

@property (nonatomic, strong) TTDetailModel *model;
@property (nonatomic, strong) TTDetailFilterCustomHeaderView *headerView;
@property (nonatomic, copy) NSString *styleLibraryId;

@property (nonatomic, strong) UIButton *timeBtn;

@property (nonatomic, strong) TTCustomizedModel *customModel;

@end

@implementation TTDetailFilterDingzhiView

- (instancetype)initWithFrame:(CGRect)frame model:(id)model styleLibraryId:(NSString *)styleLibraryId
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.styleLibraryId = styleLibraryId;
        
        self.model = model;
        
        
        [self getDingzhiFilterDatas];
    }
    return self;
}

- (void)getDingzhiFilterDatas
{
    NSDictionary *dic = @{
        @"param.styleLibraryId" : self.styleLibraryId,
        @"param.categoryName" : self.model.CategoryName
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getCustomizedFilter_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        self.customModel = [TTCustomizedModel modelWithJSON:responseObject];
        
        [self setupSubviews];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self];
}

// 是否能提交
- (BOOL)canCommit
{
    if (self.customModel.InsertSizeList.count > 0 && self.dic[@"param.insertSize"] == nil) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请选择镶口"];
        return NO;
    }
    
    if (self.customModel.HandSizeList.count > 0 && self.dic[@"param.handSize"] == nil) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请选择手寸"];
        return NO;
    }
    
    if (self.customModel.BraceletSizeList.count > 0 && self.dic[@"param.braceletSize"] == nil) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请选择内径"];
        return NO;
    }
    
    if (self.customModel.GoldColorList.count > 0 && self.dic[@"param.goldColor"] == nil) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请选择成色"];
        return NO;
    }
    if (self.timeLabel.text.length == 0) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"请选择收货时间"];
        return NO;
    }
    
    return YES;
}

- (void)setupSubviews
{
    [self addSubview:self.headerView];
    
    self.headerView.top = 10;
    
    UILabel *textLabel = [UILabel labelWithTitle:@"刻字" color:UIColorHex(#333333) fontSize:12];
    [self addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.equalTo(self.headerView.mas_bottom).mas_offset(30);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    self.tf = [[UITextField alloc] init];
    self.tf.backgroundColor = UIColorHex(#F0F4F7);
    self.tf.layer.cornerRadius = 5;
    self.tf.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.tf];
    [self.tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textLabel.mas_right);
        make.height.mas_equalTo(30);
        make.right.equalTo(self).mas_offset(-10);
        make.top.equalTo(textLabel);
    }];
    
    UILabel *textLabel2 = [UILabel labelWithTitle:@"*收货时间" color:UIColorHex(#333333) fontSize:12];
    [self addSubview:textLabel2];
    [textLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(textLabel);
        make.top.equalTo(textLabel.mas_bottom).mas_offset(10);
    }];
    
    self.timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.timeBtn.backgroundColor = UIColorHex(#F0F4F7);
    self.timeBtn.layer.cornerRadius = 5;
    [self.timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.timeBtn setImage:[UIImage imageNamed:@"cellarrow_3"] forState:UIControlStateNormal];
    [self.timeBtn addTarget:self action:@selector(timeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.timeBtn];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.tf);
        make.top.equalTo(textLabel2);
    }];
    
    self.timeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#666666) fontSize:12];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textLabel2.mas_right).mas_offset(10);
        make.right.height.equalTo(self.tf);
        make.top.equalTo(textLabel2);
    }];
    
    UILabel *textLabel3 = [UILabel labelWithTitle:@"备注" color:UIColorHex(#333333) fontSize:12];
    [self addSubview:textLabel3];
    [textLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(textLabel);
        make.top.equalTo(textLabel2.mas_bottom).mas_offset(10);
    }];
    
    self.textView = [[UITextView alloc] init];
    self.textView.layer.cornerRadius = 5;
    self.textView.backgroundColor = UIColorHex(#F0F4F7);
    [self addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.tf);
        make.height.mas_equalTo(100);
        make.top.equalTo(textLabel3);
    }];
    
    
}

///日期
- (void)timeBtnClick
{
    [self endEditing:YES];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd"];
    NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    
    NSDate *afterDate = [[NSDate date] dateByAddingDays:15];
    
    @weakify(self)
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay minLimitDateString:nowDate maxLimitDateString:@"2100-1-1" scrollToDate:afterDate CompleteBlock:^(NSDate *selectDate) {
        @strongify(self)
        NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        self.timeLabel.text = date;
        [self.dic setObject:date forKey:@"param.receiveTime"];
    }];
    datepicker.doneButtonColor = kMainColor;
    datepicker.dateLabelColor = kMainColor;
    datepicker.hideBackgroundYearLabel = YES;
    [datepicker show];
}



- (TTDetailFilterCustomHeaderView *)headerView
{
    if (!_headerView) {
        @weakify(self)
        _headerView = [TTDetailFilterCustomHeaderView customHeaderViewWithCustomizedModel:self.customModel selectFinished:^(NSMutableDictionary * _Nullable dic) {
            @strongify(self)
            self.dic = dic;
            NSLog(@">>> dic = %@", self.dic);
        }];
        [self addSubview:_headerView];
    }
    return _headerView;
}



@end
